package com.kgcx.system.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.common.utils.CollectionUtils;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.system.domain.AppVersion;
import com.kgcx.system.mapper.AppVersionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 吴刚林
 * @date 2017/11/20
 */
@Service
public class AppVersionService {

    @Autowired
    private AppVersionMapper appVersionMapper;


    public AppVersion getAppVersion(Long kid) {
        return appVersionMapper.selectByPrimaryKey(kid);

    }

    public List<AppVersion> getAppVersionList(RequestData rd) {
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        return appVersionMapper.selectAllActive();

    }

    public AppVersion getLastAppVersion(Integer type) {
        AppVersion appVersion = new AppVersion();
        appVersion.setType(type);
        PageHelper.orderBy("version_no desc");
        List<AppVersion> appVersions = appVersionMapper.selectActive(appVersion);
        return CollectionUtils.getFirst(appVersions);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addAppVersion(AppVersion appVersion) {
        appVersionMapper.insertSelective(appVersion);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateAppVersion(AppVersion appVersion) {
        appVersionMapper.updateByPrimaryKeySelective(appVersion);

    }

    @Transactional(rollbackFor = Exception.class)
    public void removeAppVersion(Long kid) {
        AppVersion appVersion = new AppVersion();
        appVersion.setKid(kid);
        appVersion.setStatus(Cons.COMMON_STATUS_DELETE);
        appVersionMapper.updateByPrimaryKeySelective(appVersion);

    }
}
